<?php

/**
 * Формы для работы с себе подобными
 */
class FormsForm extends Zend_Form {

    /**
     * Форма
     *
     * @param int $fid #ID Формы
     * @return FormsForm
     */
    public function Forms($fid = 0) {
        $this->setName('forms');
        $t = Zend_Registry::get('Zend_Locale');
        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $description = new Zend_Form_Element_Textarea('description');
        $description->setLabel('Decription')
                ->addValidator('NotEmpty');

        $captcha = new Zend_Form_Element_Checkbox('captcha');
        $captcha->setLabel('Captcha')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $button = new Zend_Form_Element_Text('button');
        $button->setLabel('Button text')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($title, $description, $captcha, $button, $submit));
        $this->setAction(ADMIN_URL . '/forms/edit/id/' . $fid);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма поля
     *
     * @param int $fid #ID Формы
     * @param int $id #ID поля
     * @return FormsForm
     */
    public function Field($fid = 0, $id = 0) {
        $this->setName('field');
        $t = Zend_Registry::get('Zend_Locale');
        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $field = new Zend_Form_Element_Text('field');
        $field->setLabel('Field name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $types = array(
            'text' => $t->_('Text'),
            'textarea' => $t->_('Textarea'),
            'wysiwyg' => $t->_('WYSIWYG'),
            'link' => $t->_('Link'),
            'image' => $t->_('Image'),
            'email' => $t->_('E-mail'),
            'date' => $t->_('Date'),
            'select' => $t->_('Select'),
            'tags' => $t->_('Tags')
        );

        $type = new Zend_Form_Element_Select('type');
        $type->addMultiOptions($types)
                ->setLabel('Type')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $required = new Zend_Form_Element_Checkbox('required');
        $required->setLabel('Required field')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $position = new Zend_Form_Element_Text('position');
        $position->setLabel('Position')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $visible = new Zend_Form_Element_Checkbox('visible');
        $visible->setLabel('Visibility')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $where = new Zend_Form_Element_Text('where');
        $where->setLabel('Where')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $table = new Zend_Form_Element_Text('table');
        $table->setLabel('Table')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $table_field = new Zend_Form_Element_Text('table_field');
        $table_field->setLabel('Table field')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $order = new Zend_Form_Element_Text('order');
        $order->setLabel('Order')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $image_width = new Zend_Form_Element_Text('image_width');
        $image_width->setLabel('Image width (px)')
                ->addFilter('StringTrim')
                ->addValidator('Digits')
                ->addValidator('NotEmpty');

        $image_height = new Zend_Form_Element_Text('image_height');
        $image_height->setLabel('Image height (px)')
                ->addFilter('StringTrim')
                ->addValidator('Digits')
                ->addValidator('NotEmpty');

        $preview = new Zend_Form_Element_Checkbox('preview');
        $preview->setLabel('Make preview?')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $preview_width = new Zend_Form_Element_Text('preview_width');
        $preview_width->setLabel('Preview width (px)')
                ->addFilter('StringTrim')
                ->addValidator('Digits')
                ->addValidator('NotEmpty');

        $preview_height = new Zend_Form_Element_Text('preview_height');
        $preview_height->setLabel('Preview height (px)')
                ->addFilter('StringTrim')
                ->addValidator('Digits')
                ->addValidator('NotEmpty');
        
        $identifier = new Zend_Form_Element_Text('identifier');
        $identifier->setLabel('Identifier')
                ->addFilter('StringTrim')
                ->addValidator('Alnum')
                ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($title, $field, $required, $visible, $position, $type, $table, $table_field, $where, $order, $image_width, $image_height, $preview, $preview_width, $preview_height, $identifier, $submit));
        $this->setAction(ADMIN_URL . '/forms/field/id/' . $id . '/fid/' . $fid);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Формирование формы
     *
     * @param int $fid #ID формы
     * @param string $action Действие формы
     * @param bool $section Возможность правки секции
     * @return FormsForm
     */
    public function getForm($fid = 0, $action = '', $section = false) {
        $this->setName('forms');
        $t = Zend_Registry::get('Zend_Locale');
        $model = new FormsModel();
        $fields = $model->getFields($fid);
        $this->setDefaultTranslator($t);
        foreach ($fields as $v) {
            switch ($v->type) {

                case 'text':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title);
                    if ($v->required)
                        $element->setRequired();
                    break;

                case 'textarea':
                    $element = new Zend_Form_Element_Textarea($v->field);
                    $element->setLabel($v->title);
                    if ($v->required)
                        $element->setRequired();
                    break;

                case 'wysiwyg':
                    $element = new Zend_Form_Element_Textarea($v->field);
                    $element->setLabel($v->title)
                            ->setAttrib('class', 'tinymce');
                    if ($v->required)
                        $element->setRequired();
                    break;

                case 'link':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title)
                            ->setAttrib('class', 'file_fm');
                    if ($v->required)
                        $element->setRequired();
                    break;
                    
                case 'image':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title)
                            ->setAttrib('class', 'file_fm');
                    if ($v->required)
                        $element->setRequired();
                    break;
                    
                case 'email':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title)
                            ->addValidator('EmailAddress');
                    if ($v->required)
                        $element->setRequired();
                    break;

                case 'date':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title)
                            ->setAttrib('class', 'datepicker');
                    if ($v->required)
                        $element->setRequired();
                    break;
                    
                case 'tags':
                    $element = new Zend_Form_Element_Text($v->field);
                    $element->setLabel($v->title)
                            ->setAttrib('class', 'tags')
                            ->setAttrib('rel', $v->identifier);
                    if ($v->required)
                        $element->setRequired();
                    break;

                case 'select':
                    $element = new Zend_Form_Element_Select($v->field);
                    $list = $model->getList($v->table, $v->table_field, $v->where, $v->order);
                    $element->setLabel($v->title);
                    $element->addMultiOptions($list);
                    if ($v->required) {
                        $element->setRequired();
                    }
                    break;
            }
            $array[] = $element;
        }
        if ($section) {
            $element = new Zend_Form_Element_Text('sid');
            $element->setLabel('sid');
            $array[] = $element;
        }
        $form = $model->getForms($fid);
        if ($form['captcha']) {
            $captcha = new Zend_Form_Element_Captcha('captcha', array(
                        'captcha' => array(
                            'captcha' => 'Image',
                            'wordLen' => 6,
                            'width' => 260,
                            'timeout' => 120,
                            'expiration' => 300,
                            'font' => PUBLIC_PATH . '/fonts/captcha.ttf',
                            'imgDir' => PUBLIC_PATH . '/tmp/',
                            'imgUrl' => '/tmp/',
                            'gcFreq' => 5
                        )
                    ));
            $captcha->setLabel('Captcha');
            $array[] = $captcha;
        }

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($form['button']);
        $submit->setAttrib('id', 'submitbutton');
        $array[] = $submit;

        $this->addElements($array);
        $this->setAction($action);
        return $this;
    }

}